const app = getApp();
const https = require('../../utils/https');
let timer = null
Page({

  /**
   * 页面的初始数据
   */
  data: {
    order_id: '', // 订单id
    house_list: [],
    checkItems: [], // 验房项目 check_result, icon id name
    status: 0,
    page: 0,
    house_name: '',
    showQrcode: false, // 展示收款码
    qrcode: 'https://wx.amagich.com/upload/native-pay/order_24_1606294135.png'
  },
  inspectionTap() {
    this.setData({
      maskShow: true,
      inspectShow: true
    })
  },
  maskTap() {
    this.setData({
      showQrcode: false,
    })
  },
  /**
   * 单个项目验收
   * @param event
   */
  checkItem(event) {
    console.log(event)
    const item_id = event.currentTarget.dataset.item_id
    const checkItem = this.data.checkItems.find(item => item.id === item_id)
    wx.navigateTo({
      url: '/pages/inspection_item/item?item_id=' + item_id + '&icon=' + checkItem.icon + '&item_name=' + checkItem.name + '&order_id=' + this.data.order_id,
    })
  },
  // 验收完成
  submitCheck() {
    var that = this,
        id = this.data.order_id,
        inspector_id = wx.getStorageSync('inspector_id');
    https.post('completeHouseCheck', {
      id,
      inspector_id
    }, false, '提交中').then(res => {
      console.log('验收结果', res)
      if(res) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 2000
        })
        this.setData({
          qrcode: res.qrCode,
          showQrcode: true
        })
        // 轮询支付结果
        this.getBookOrderInfo()
      }
    })
  },
  // 获取订单信息
  /**
   * @xk 获取订单信息
   */
  getBookOrderInfo: function () {
    var that = this,
        order_sn = this.data.order_sn;
    https.post('getBookOrderInfo', {
      order_sn: order_sn
    }, false, '加载中').then(res => {
      console.log(res.balance_paid)
      if(res && res.balance_paid === 1) {
        wx.showToast({
          title: '订单已支付',
          icon: 'none',
          duration: 2500
        })
        // setTimeout(() => {
        //   wx.navigateBack({delta: 2})
        // }, 1500)
      } else {
        timer = setTimeout(() => {
          this.getBookOrderInfo()
        },1200)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      is_login_inspector = wx.getStorageSync('is_login_inspector');
    if (!is_login_inspector || is_login_inspector != 1) {
      wx.showToast({
        title: '请先登陆验房师账号',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({
        delta: 0,
      })
      return false;
    }
    if (options) {
      that.setData({
        order_sn: options.order_sn,
        order_id: options.order_id
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      house_list: [],
      page: 0,
    })
    this.getBookOrderItems(this.data.order_id)
    // this.getHouseList();
  },
  /**
   * 获取验房项目
   * @param orderId 订单id
   */
  getBookOrderItems(orderId) {
    wx.request({
      url: 'https://wx.amagich.com/' + 'v1/' + 'getBookOrderItems',
      data: {
        id: orderId,
      },
      header: {
        "Content-Type": "application/json"
      },
      method: 'GET',
      success: res => {
        console.log(res.data.resultData)
        this.setData({
          checkItems: res.data.resultData
        })
      }})
  },

  /**
   * @xk 获取订单已经创建的房间信息，以及验房信息
   */
  getHouseList: function (res) {
    var that = this,
      house_list = this.data.house_list,
      page = this.data.page,
      order_sn = this.data.order_sn;
    https.post('getHouseList', {
      order_sn: order_sn,
      page: page,
      is_page: 1
    }, false, '加载中').then(res => {
      if (res.list) {
        house_list.push.apply(house_list, res.list);
      }
      that.setData({
        house_list: house_list,
        page: res.page
      })
    })
  },

  /**
   * @xk 输入框
   */
  chanageHouseName: function (obj) {
    this.setData({
      house_name: obj.detail.value
    })
  },
  sureEmail: function (obj) {
    var that = this,
      house_name = this.data.house_name,
      order_sn = this.data.order_sn,
      inspector_id = wx.getStorageSync('inspector_id');
    if (house_name == '') {
      wx.showToast({
        title: '请填写房间名称',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    https.post('saveHouseList', {
      house_name: house_name,
      order_sn: order_sn,
      inspector_id: inspector_id
    }, false, '正在保存').then(res => {
      if (res) {
          that.setData({
            maskShow: false,
            inspectShow: false,
            house_id: res,
          })
          wx.navigateTo({
            url: '/pages/inspection_project/project?house_id=' + res,
          })
      }
    })
  },
  /**
   * @xk 删除房间
   */
  delHome: function (obj) {
    var that = this,
      house_id = obj.currentTarget.dataset.house_id;
    wx.showModal({
      title: '删除房间确认',
      content: '删除后所有验房记录同步删除,您确定要删除该房间吗？',
      success: function (sm) {
        if (sm.confirm) {
          // 用户点击了确定 可以调用删除方法了
          https.post('delHouse', {
            house_id: house_id
          }, false, '正在删除').then(res => {
            that.setData({
              house_list: [],
              page: 0,
            })
            that.getHouseList();
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * @xk 全部验房完毕
   */
  overCheck: function () {
    var that = this,
      order_sn = this.data.order_sn;
    wx.showModal({
      title: '全部完成确认',
      content: '请确认所有房间均已完成验收？',
      success: function (sm) {
        if (sm.confirm) {
          https.post('overCheckHouse', {
            order_sn: order_sn
          }, false, '全部验房完毕').then(res => {
            wx.navigateBack({
              delta: 0,
            })
          })
        } else if (sm.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      checkItems: [],
    })
    this.onShow();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getHouseList();
  },

})
